﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class NewsDal
    {
        DBHepler hepler = new DBHepler();

        /// <summary>
        /// 查询所有新闻
        /// </summary>
        /// <returns></returns>
        public DataTable GetNewsAll(int pageNow)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@pageNow",pageNow)
                                        };
            return hepler.GetDataTableByProc("sp_GetNewsAll",parameters);
        }

        public DataTable GetNewsAll2()
        {
            return hepler.GetDataTableByProc("sp_GetNewsAll2");
        }

        /// <summary>
        /// 获取前几条新闻
        /// </summary>
        /// <returns></returns>
        public DataTable GetTopNews()
        {
            return hepler.GetDataTableByProc("sp_GetTopNews");
        }

        //返回新闻的条数
        public int GetNewsAllCount()
        {
            int result = int.Parse(hepler.GetExecuteScalar("sp_GetNewsAllCount").ToString());
            return result;
        }

        public int GetNewsCountByCategory(int categoryId)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@categoryId",categoryId)
                                        };
            int result = int.Parse(hepler.GetExecuteScalar("sp_GetNewsCountByCategory", parameters).ToString());
            return result;
        }

        /// <summary>
        /// 根据分类，查询出新闻
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public DataTable GetNewsByCategory(int categoryId,int pageNow)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@categoryId",categoryId),
                                            new SqlParameter("@pageNow",pageNow)
                                        };
            return hepler.GetDataTableByProc("sp_GetNewsByCategory",parameters);
        }

        public DataTable GetNewsByCategory2(int categoryId)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@categoryId",categoryId)

                                        };
            return hepler.GetDataTableByProc("sp_GetNewsByCategory2", parameters); 
        }

        /// <summary>
        /// 返回具体新闻信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetNewsContent(int id)
        {
            SqlParameter[] parameter = {
                                        new SqlParameter("@id",id)
                                       };
            return hepler.GetDataTableByProc("sp_GetNewsContent",parameter);
        }

        /// <summary>
        /// 增加新闻信息
        /// </summary>
        /// <param name="news"></param>
        /// <returns></returns>
        public bool AddNews(Model.News news)
        {
            SqlParameter[] parameter = {
                                            new SqlParameter("@categoryId",news.CategoryId),
                                            new SqlParameter("@title",news.Title),
                                            new SqlParameter("@content",news.Content),
                                            new SqlParameter("@time",news.Time),
                                            new SqlParameter("@author",news.Author)
                                       };
            if (hepler.InsertByProc("sp_AddNews", parameter) == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到新闻的分类信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetNewsCategory()
        {
            return hepler.GetDataTableByProc("sp_GetNewsCategoryAll");
        }

        /// <summary>
        /// 更新新闻信息
        /// </summary>
        /// <param name="news"></param>
        /// <returns></returns>
        public bool UpdateNews(Model.News news)
        {
            SqlParameter[] parameters ={
                                        new SqlParameter("@id",news.Id),
                                        new SqlParameter("@categoryId",news.CategoryId),
                                        new SqlParameter("@content",news.Content),
                                        new SqlParameter("@title",news.Title)
                                      };
            if (hepler.UpdateByProc("sp_UpdateNewsContent", parameters) == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool DeleteNewsContent(int id)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@id",id)
                                         };
            if (hepler.DeleteByProc("sp_DeleteNewsContent", parameters) == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
